package com.anta.it.hr.devtools.core.config;

import com.anta.it.hr.devtools.core.request.RequestInetAddress;
import com.anta.it.hr.devtools.core.rule.WhitelistRule;
import com.netflix.loadbalancer.IRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * 此处无需加@Configuration注解，否则它将被所有的@RibbonClient配置继承
 * @author wangxiaoxian
 * @since 2021/7/7
 */
public class RibbonRuleConfig {

    @Bean
    public IRule ribbonRule(@Autowired DiscoveryClient discoveryClient,
                            @Autowired RequestInetAddress requestInetAddress) {
        WhitelistRule whitelistRule = new WhitelistRule();
        whitelistRule.setDiscoveryClient(discoveryClient);
        whitelistRule.setAddress(requestInetAddress);
        return whitelistRule;
    }
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
